package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entitry.ShoppingCart;
import com.itheima.reggie.entitry.User;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {


    /**
     * 添加购物车
     */

    @Autowired
    ShoppingCartService shoppingCartService;
    @PostMapping("/add")
    public R<ShoppingCart> insert(@RequestBody ShoppingCart shoppingCart, HttpSession session){

        //获取当前用户ID
        User user = (User) session.getAttribute("user");

        shoppingCart.setUserId(user.getId());
        //查询数据库中是否存在该用户的该购物车信息

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,shoppingCart.getUserId())
                .eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
                //.eq(StringUtils.isNotEmpty(shoppingCart.getDishFlavor()),ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        ShoppingCart getOne = shoppingCartService.getOne(lqw);

        //如果当前购物车没有此信息，就新增，如果有，就数量加1
        if (getOne == null){
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }else {
            getOne.setNumber(getOne.getNumber() + 1);
            shoppingCartService.updateById(getOne);
            return R.success(getOne);
        }



    }

    /**
     * 查询购物车信息
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> listAll(HttpSession session){

        //获取当前用户
        User user = (User) session.getAttribute("user");
        System.out.println("========================================" + user);
        //获取当前用户的购物车信息
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,user.getId());
        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return R.success(list);
    }

    /**
     * 删除购物车
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> delete(@RequestBody ShoppingCart shoppingCart,HttpSession session){

        //获取当前用户
        User user = (User) session.getAttribute("user");

        //获取当前用户要删除的购物车信息
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,user.getId());


        if (shoppingCart.getDishId() != null){
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }

        if (shoppingCart.getSetmealId() != null){
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }


        ShoppingCart one = shoppingCartService.getOne(lqw);

        //判断数量，为1时，删除当前购物车信息，不为1时，减少数量
        if (one.getNumber() == 1){
            one.setNumber(0);
            shoppingCartService.remove(lqw);
        }else{
            one.setNumber(one.getNumber() - 1);
            shoppingCartService.updateById(one);
        }

        return R.success(one);
    }

    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session){
        //获取当前用户
        User user = (User) session.getAttribute("user");

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        //删除当前用户的购物车信息
        lqw.eq(ShoppingCart::getUserId,user.getId());
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功！");
    }
}
